package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.StrategyQuery;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.SevenUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/strategy")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private ITravelService travelService;
    //待发布状态的游记管理
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo")StrategyQuery qo, Model model){
        model.addAttribute("result",strategyService.query(qo));
        model.addAttribute("regions",regionService.list());
        return "strategy/list";
    }
    //保存或者修改推荐游记
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy, MultipartFile file){
        JSONResult jsonResult = new JSONResult();
        try {
            //保存照片
            if (file !=null && file.getSize()>0){
                String filePath = SevenUploadUtil.uploadQiniu(file);
                strategy.setCoverUrl(filePath);
            }
            strategyService.saveOrUpdate(strategy);
        }catch (Exception e){
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }
    //根据攻略查询指定的游记
    @RequestMapping("/lookList")
    public String travelsByStrategyId(Long strategyId, Model model){

        model.addAttribute("travels",travelService.gettravelsByStrategyId(strategyId));
        model.addAttribute("strategys",strategyService.list());
        model.addAttribute("strategyId",strategyId);
        return "lookList/list";
    }
    //删除关系
    @RequestMapping("/deleteRelation")
    public String deleteRelation(Long strategyId, Long travelId){
        travelService.deleteRelation(strategyId,travelId);
        return "redirect:/strategy/lookList.do?strategyId="+strategyId;
    }
    //保存关系
    @RequestMapping("/saveRelation")
    @ResponseBody
    public Object saveRelation(Long strategyId, Long travelId){
        JSONResult jsonResult = new JSONResult();
        try {
            travelService.saveRelation(strategyId,travelId);
        }catch (Exception e){
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }
}
